<?php

class Yar_Api extends yar_Basic {

    private $m_sms;

    public function __construct(){
        // 验证签名
        if(!$this->verifySign()){
            $rep['code'] = 1001;
            $rep['msg'] = '非法用户！';

            echo $this->response($rep);
            die();
        }
    }

    //动态调用API的各种方法
    public function toYar($action = "", $option = []) {
        //如果没有传递过来方法的话不允许通过
        if(!$action){
            return false;
        }
        //如果该类中并没有该方法的话也不执行
        if(!method_exists(__CLASS__, $action)){
            return false;
        }
        
        return $this->$action($option);
    }
    
    //发送短信
    public function sendsms($option = []){
        if(empty($option) || !is_array($option)){
            return false;
        }
        
        $phone = isset($option['phone']) ? $option['phone'] : '';
        $data = isset($option['data']) ? $option['data'] : '';
        
        if(!$phone || !$data){
            return false;
        }
        
        $this->m_sms = Helper::load('Sms');
        
        $iData['content'] = $data;
        $iData['phone'] = $phone;
        $iData['send_time'] = CUR_TIMESTAMP;

        $this->m_sms->Insert($iData);
        
        if(ENV == 'PRODUCT'){//正式环境真实发送
            $target = SMS_URL;
            $post_data = "account=".SMS_ACCOUNT."&password=".SMS_PWD."&mobile=".$phone."&content=".rawurlencode($data);
            //密码可以使用明文密码或使用32位MD5加密
            Post($post_data, $target);
        }
        
        $rep['code'] = 1;
        $rep['msg'] = '提交成功！';
        
        // 返回数据用 response, 见 Yar_Basic.php
        return $this->response($rep);
    }
    
}